﻿/*
创建人：longb
日期：2022-06-16
描述：试车返回单js
*/
var page = {
    pagetotal: 0,
    pageSize: 20,
    pageNumber: 1,
    pageType: getParam("type"), //0：创建 1：审核
    rootPath: getRootPath(), //网站根路径
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSTestRunNoticeBillHandler.ashx",
    ajaxUrl2: getRootPath() + "/Ashx/ZsHandler/ZSTestRunReturnBillHandler.ashx",
    comboxUrl:getRootPath() + "/Ashx/ZsHandler/ZSTestRunNoticeBillHandler.ashx",//combox下拉-调用后台的URL
    comboxAction:"GetMfgorder",//combox下拉-调用后台的具体方法
    //初始化页面
    init: function () {
        page.bind();
        customizedEasyuiPagination('pp'); //定制化分页
        // $("#EditorpanelSection").hide();
        $('#Editor').hide();
        $("#CFJForm").hide();
        $("#WPCForm").hide();
        $(".EXCLUDEMETHODS").hide();
        page.InitData();
        if (page.pageType == "0") {
            $("#SaveCreate").css("display", "");
            //$("#EditorpanelSection").show();
            $('#Editor').show();
            $("#CFJForm").show();
            $("#WPCForm").show();
            $('#MFGORDERNAME').next().hide();
            $('#WORKCENTERNAME').next().hide();
            $('#RESOURCENAME').next().hide();
        }
        if (page.pageType == "1") {
            $("#ConfirmRetrun").css("display", "");
            $('.EditorpanelSection').panel("setTitle","查看区域");
            page.DisableForm();
        }
        if (page.pageType == "2") {
            $("#Recieve").css("display", "");
            $("#WPCForm").show();
            $('#TRReturnUFBill').show();
            $('#FAULTTYPE').textbox("readonly");
            $('#FAULTPERFORM').textbox("readonly");
            $(".EXCLUDEMETHODS").show();
            $("#DaleteTRReturnUFBillData").hide();
            $('#SaveTRReturnUFBillData').linkbutton({ text: "保存" });
            $('.EditorpanelSection').panel("setTitle","查看区域");
           
            page.DisableForm();
        }
    },
    DisableForm:function(){//表单编辑框禁止编辑
    $('#TESTFINISHDATE').next().find('.textbox-icon').hide();
     $('#TestRunType').next().find('.textbox-icon').hide();
      $('#DutyWorkcenterID').next().hide();
       $('#TestBed').next().hide();
         $('.fontColor').css("color","black");
         $('#mfgCombox').hide();
         $('#btnComboxSearch').linkbutton('enable');
         $('#txtCombox').textbox('readonly');
         $('#TESTFINISHDATE').textbox('readonly');
         $('#TestRunType').textbox('readonly');
         $('#DutyWorkcenterID').textbox('readonly');
         $('#TestBed').textbox('readonly');
         $('#TURBINENO').textbox('readonly');
         $('#GREASEEXPEND').textbox('readonly');
         $('#DYTCGX').textbox('readonly');
         $('#GYTCGX').textbox('readonly');
         $('#DYTCQD').textbox('readonly');
         $('#DYTCJQD').textbox('readonly');
         $('#GYTCYZ').textbox('readonly');
         $('#GYTCJYF').textbox('readonly');
         $('#TOTALWORKMINUTES').textbox('readonly');
         $('#WORKMAXLARGESTATE').textbox('readonly');
         $('#MAXLARGET').textbox('readonly');
         $('#MAXLARGEF').textbox('readonly');
         $('#MAXLARGEVIBRATE').textbox('readonly');

          // $('#EditorForm').find('.textbox-icon').hide();
         //$('#DutyWorkcenterID').next().find('.textbox-icon').hide();
    },
    //页面事件绑定
    bind: function () {
        var Initid = 0;
        var data = [{ id: 0, text: '待提交' }, { id: 1, text: '待审核' }, { id: 5, text: '待接收' }, { id: 10, text: '已接收' }, { id: 15, text: '已完成'}];
        if (page.pageType == "1") {
            data = [{ id: 1, text: '待审核' }, { id: 5, text: '待接收' }, { id: 10, text: '已接收' }, { id: 15, text: '已完成'}];
            Initid = 1;
        }
        if (page.pageType == "2") {
            data = [{ id: 5, text: '待接收' }, { id: 10, text: '已接收' }, { id: 15, text: '已完成'}];
            Initid = 5;
        }
        $('#Status').combobox({
            data: data,
            valueField: 'id',
            textField: 'text'
        });
        $('#TestRunType').combobox({
            data: [{ id: "0", text: '工厂试车' }, { id: "1", text: '检验试车' }, { id: "2", text: '附加试车'}],
            valueField: 'id',
            textField: 'text'
        });
        $('#Status').combobox('setValue', Initid);
        //$('#TestRunType').combobox('setValue', 0);
           $('#mfgCombox').selectCombox({
            search: function () { },
            cancel: function () { $('#MFGORDERID').val(''); $('#ATTRIBUTEVALUE').textbox('setValue','');},
            selected: function (val, text,record){
                $('#MFGORDERID').val(val);
                $('#txtCombox').textbox('setValue',text);
                $('#ATTRIBUTEVALUE').textbox('setValue', record.ATTRIBUTEVALUE);//发动机号
                $('#PRODUCTFAMILYNAME').textbox('setValue', record.PRODUCTFAMILYNAME);//型号
                $('#divComboxTxt').show();
                $('#divComboxSelect').hide();
                $('#divComboxSearch').show();
                $('#divComboxClear').hide();
            }
        });
        //任务单信息
        $("#ReturnBillTable").datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            rownumbers: true,
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'ZSTESTRUNRETURNBILLID', width: 120, checkbox: true },
                 { field: 'ZSTESTRUNRETURNBILLNO', title: '试车返回单号', width: 120 },
                 { field: 'MFGORDERNAME', title: '订单号', width: 150 },
                 { field: 'ATTRIBUTEVALUE', title: '发动机号', width: 120 },
		         { field: 'PRODUCTFAMILYNAME', title: '型号', width: 130 }, //--
                 {field: 'TESTFINISHDATE', title: '装配完工日期', width: 90 },
                 { field: 'TESTRUNTYPENAME', title: '试车类型', width: 120, },
                 { field: 'WORKCENTERNAME', title: '责任单位', width: 100 },
                 { field: 'RESOURCENAME', title: '试车台', width: 100 },
                 { field: 'STATUS', title: '试车返回单状态', width: 100, },
                 //{ field: 'NOTES', title: '备注说明', width: 100 },
                 { field: 'BILLTYPE', title: '来源', width: 100, align: 'center' },
                 { field: 'CREATEEMPLOYEE', title: '创建人', width: 100, },
                 { field: 'CREATEDATE', title: '创建时间', width: 100 },
                 { field: 'AUDITEMPLOYEE', title: '审核人', width: 100,  },
                 { field: 'AUDITDATE', title: '审核时间', width: 100 },
                 { field: 'RECIEVEEEMPLOYEE', title: '接收人', width: 100,  },
                 { field: 'RECIEVEDATE', title: '接收时间', width: 100 },
                 { field: 'XC', title: '', width: 50 }
	           ]],
            onSelect: function (index, row) {
                page.GetTRReturnCFBillData();
                page.GetTRReturnUFBillData();
                row["MFGORDERNO"] = row.MFGORDERID;
                $('#EditorForm').form('load', row);
                if(!$('#btnComboxSearch').hasClass('l-btn-disabled')){//防止连续行点击，查询按钮事件失效
                    $('#txtCombox').combobox('disable');
                    $('#btnComboxSearch').linkbutton('disable');
                }
                 $("#ReturnBillTable").datagrid("uncheckAll").datagrid("checkRow",index);
                if(row.STATUS!="待提交"){//所选行状态不是【待提交】，则细表新增删除隐藏
                 $('.TRReturnCFBill').hide();
                 $('.TRReturnUFBill').hide();
                }else{
                 $('.TRReturnCFBill').show();
                 $('.TRReturnUFBill').show();
                }
                if(page.pageType=="2"&&row.STATUS=="待接收"){
                 $('.TRReturnUFBill').show();
                }
            },
             onCheckAll:function(rows){
                $("#ReturnBillTable").datagrid("clearSelections");
                $('#EditorForm').form("clear");
                $('#CFJTable').datagrid("loadData",[]);
                $('#WPCTable').datagrid("loadData",[]);
            },
            onCheck:function(rowIndex,rowData){
                var checkRows= $("#ReturnBillTable").datagrid("getChecked");
                var selectRow= $("#ReturnBillTable").datagrid("getSelected");
                if(checkRows.length>1){
                    $("#ReturnBillTable").datagrid("clearSelections");
                     $('#EditorForm').form("clear");
                     $('#CFJTable').datagrid("loadData",[]);
                     $('#WPCTable').datagrid("loadData",[]);
                }
            }
        });

        $('#CFJTable').datagrid({
            rownumbers: true,
            columns: [[
                 { field: 'ZSTRETURNCFBILLID', width: 50, checkbox: true },
                 { field: 'DEPENDTYPE', title: '附件型别', width: 100 },
                 { field: 'PREBATCHNO', title: '换下批次号', width: 100 },
                 { field: 'AFTERBATCHNO', title: '换上批次号', width: 100 },
		         { field: 'PREWORKMINS', title: '换下附件已工作时间（min）', width: 170 }, //--
                 {field: 'CHANGEREASON', title: '换下原因', width: 120 }
	           ]]
        });
        $('#WPCTable').datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            //rownumbers: true,
            columns: [[
                 { field: 'ZSTRRETURNUFBILLID', width: 120, checkbox: true },
                 { field: 'SERIALNUMBER', title: '序号', width: 50, align: 'center' },
                 { field: 'FAULTTYPE', title: '故障类型', width: 80 },
                 { field: 'FAULTPERFORM', title: '故障现象', width: 100 },
		         { field: 'CREATENAME', title: '检验员', width: 80 }, //--
                 { field: 'CREATEDATE', title: '创建时间', width: 100 },
                 { field: 'EXCLUDEMETHODS', title: '排除方法', width: 120 },
                 { field: 'EXCLUDENAME', title: '技术员', width: 80, align: 'center' },
                 { field: 'EXCLUDEDATE', title: '保存时间', width: 100 }
	           ]],
            onSelect: function (index, row) {
                if (page.pageType == "2") {
                    $('#WPCForm').form('load', row);
                }
            },
            onLoadSuccess:function(){
                if(page.pageType == "2"){
                $('#WPCTable').datagrid("hideColumn","ZSTRRETURNUFBILLID");
                }
            }
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.GetReturnBillData();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.GetReturnBillData();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.reset(true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });
    },
    //初始化下拉选择数据
    InitData: function () {
        page.post("", { action: "GetDutyWorkcenter" }, function (res) {
            $('#DutyWorkcenterID').combobox({
                data: res.DBTable,
                valueField: 'WORKCENTERID',
                textField: 'WORKCENTERNAME'
            });
        });
        page.post("", { action: "GetResourceDef" }, function (res) {
            $('#TestBed').combobox({
                data: res.DBTable,
                valueField: 'RESOURCEID',
                textField: 'RESOURCENAME'
            });
        });
    },
    //查询
    searchData: function () {
        page.reset(false, true);
        page.pageNumber = 1;
        page.GetReturnBillData();
        $('#EditorForm').form("clear");
        $('#MFGORDERNO').combobox('enable');
        $('#CFJTable').datagrid('loadData', []);
        $('#WPCTable').datagrid('loadData', []);
    },
    //查询试车通知单信息
    GetReturnBillData: function () {
        var frdata = $('#searchForm').serializeArray();
        var data = { ZSTESTRUNRETURNBILLID: '' };
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "GetReturnBillData",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            data: JSON.stringify([data])
        };
        page.post("1", param, function (res) {
            if (res.Result) {
                //console.info(res);
                page.pagetotal = res.RowCount;
                $('#ReturnBillTable').datagrid('loadData', res.DBTable);
            }
        });
    },
    //保存试车通知单信息
    SaveReturnBillData: function () {
        var frdata = $('#EditorForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "SaveReturnBillData",
            data: JSON.stringify([data])
        };
        if(data.STATUS!="待提交"&&data.STATUS!=""){displayMessage("返回单"+data.STATUS+",不能再保存",false);return;}
        if (data.MFGORDERID == "") { displayMessage("订单号不能为空，请选择", false); return; }
        if (data.TESTFINISHDATE == "") { displayMessage("装配完工日期不能为空，请选择", false); return; }
        if (data.TESTRUNTYPE == "") { displayMessage("试车类型不能为空，请选择", false); return; }
        if (data.DUTYWORKCENTERID == "") { displayMessage("责任单位不能为空，请选择", false); return; }
        if (data.TESTBED == "") { displayMessage("试车台不能为空，请选择", false); return; }
        page.post("1", param, function (res) {
            if (res.Result) {
                page.GetReturnBillData();
                page.CreateReturnBillData();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //新建试车通知单信息
    CreateReturnBillData: function () {
        $('#EditorForm').form("clear");
        $('#txtCombox').combobox('enable');
        $('#btnComboxSearch').linkbutton('enable');
        $('#ReturnBillTable').datagrid('unselectAll');
    },
    //提交审核试车通知单信息
    SubmitReturnBillData: function () {
        page.ReturnBillData("SubmitReturnBillData");
    },
    //删除试车通知单信息
    DeleteReturnBillData: function () {
        page.ReturnBillData("DeleteReturnBillData");
    },
    //审核试车通知单信息
    ConfirmReturnBillData: function () {
        page.ReturnBillData("ConfirmReturnBillData");
    },
    //退回试车通知单信息
    RetrunReturnBillData: function () {
        page.ReturnBillData("ReturnBillData");
    },
    //接收试车通知单信息
    RecieveReturnBillData: function () {
        page.ReturnBillData("RecieveReturnBillData");
    },
    FinishReturnBillData: function () {
        page.ReturnBillData("FinishReturnBillData");
    },
    //
    ReturnBillData: function (action) {
        var ids = [];
        var rows = $('#ReturnBillTable').datagrid("getChecked");
        $.each(rows, function (index, item) {
            ids.push(item.ZSTESTRUNRETURNBILLID);
        });
        var param = {
            action: action,
            ids: ids.join(',')
        };
        if (ids.length == 0) { displayMessage("请勾选记录再进行操作", false); return; }

        if(page.pageType=="0"){
            if(action=="SubmitReturnBillData"&&rows[0].STATUS!="待提交"){displayMessage("所选返回单"+rows[0].STATUS+",不能再提交",false);return;}
            if(action=="DeleteReturnBillData"&&rows[0].STATUS!="待提交"){displayMessage("所选返回单"+rows[0].STATUS+",不能删除",false);return;}
        }
        if(page.pageType=="1"){
            if(action=="ConfirmReturnBillData"&&rows[0].STATUS!="待审核"){displayMessage("所选返回单"+rows[0].STATUS+",不能再审核",false);return;}
            if(action=="ReturnBillData"&&rows[0].STATUS!="待审核"){displayMessage("所选返回单"+rows[0].STATUS+",不能回退",false);return;}
        }
        if(page.pageType=="2"){
            if(action=="RecieveReturnBillData"&&rows[0].STATUS!="待接收"){displayMessage("所选返回单"+rows[0].STATUS+",不能再接收",false);return;}
            if(action=="FinishReturnBillData"&&rows[0].STATUS!="已接收"){displayMessage("所选返回单"+rows[0].STATUS+",不能再进行操作",false);return;}
        }

        page.post("1", param, function (res) {
            if (res.Result) {
                page.GetReturnBillData();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //打印试车通知单信息
    PrintReturnBillData: function () {

    },
    //查询成附件
    GetTRReturnCFBillData: function () {
        var row = $("#ReturnBillTable").datagrid("getSelected");
        var param = {
            action: "GetTRReturnCFBill",
            ZSTESTRUNRETURNBILLID: row.ZSTESTRUNRETURNBILLID
        };
        page.post("1", param, function (res) {
            $('#CFJTable').datagrid('loadData', res.DBTable);
            //displayMessage(res.Message, res.Result);
        });
    },
    //保存成附件
    SaveTRReturnCFBillData: function () {
        var row = $("#ReturnBillTable").datagrid("getSelected");
        if (row == null) { displayMessage("请选择一条返回单信息，再进行操作", false); return; }
        var frdata = $('#CFJForm').serializeArray();
        var data = { "ZSTESTRUNRETURNBILLID": row.ZSTESTRUNRETURNBILLID };
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "InsertTRReturnCFBill",
            data: JSON.stringify([data])
        };
        if (data.DEPENDTYPE == "") { displayMessage("附件型别不能为空", false); return; }
        if (data.PREBATCHNO == "") { displayMessage("换下批次号不能为空", false); return; }
        if (data.AFTERBATCHNO == "") { displayMessage("换上批次号不能为空", false); return; }
        if (data.PREWORKMINS == "") { displayMessage("已工作时间不能为空", false); return; }
        if (data.CHANGEREASON == "") { displayMessage("换下原因不能为空", false); return; }
        page.post("1", param, function (res) {
            if (res.Result) {
                page.GetTRReturnCFBillData();
                $('#CFJForm').form('clear');
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //删除成附件
    DeleteTRReturnCFBillData: function () {
        var ids = [];
        var rows = $('#CFJTable').datagrid("getChecked");
        $.each(rows, function (index, item) {
            ids.push(item.ZSTRETURNCFBILLID);
        });
        var param = {
            action: "DeleteTRReturnCFBillData",
            ids: ids.join(',')
        };
        if (ids.length == 0) { displayMessage("请勾选记录再进行操作", false); return; }
        page.post("1", param, function (res) {
            if (res.Result) {
                page.GetTRReturnCFBillData();
            }
            displayMessage(res.Message, res.Result);
        });

    },
    //查询未排除的故障
    GetTRReturnUFBillData: function () {
        var row = $("#ReturnBillTable").datagrid("getSelected");
        var param = {
            action: "GetTRReturnUFBillData",
            ZSTESTRUNRETURNBILLID: row.ZSTESTRUNRETURNBILLID
        };
        page.post("1", param, function (res) {
            $('#WPCTable').datagrid('loadData', res.DBTable);
            $('#WPCForm').form('load', { SERIALNUMBER: res.DBTable.length + 1, FAULTTYPE: '', FAULTPERFORM: '',EXCLUDEMETHODS:'' });
            //displayMessage(res.Message, res.Result);
        });
    },
    //保存未排除的故障
    SaveTRReturnUFBillData: function () {
        var row = $("#ReturnBillTable").datagrid("getSelected");
        if (row == null) { displayMessage("请选择一条返回单信息，再进行操作", false); return; }
        var frdata = $('#WPCForm').serializeArray();
        var data = { "ZSTESTRUNRETURNBILLID": row.ZSTESTRUNRETURNBILLID };
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        if (data.SERIALNUMBER == "") { displayMessage("序号不能为空", false); return; }
        if (data.FAULTTYPE == "") { displayMessage("故障类型不能为空", false); return; }
        if (data.FAULTPERFORM == "") { displayMessage("故障现象不能为空", false); return; }
        var param = {
            action: "SaveTRReturnUFBillData",
            data: JSON.stringify([data])
        };
        page.post("1", param, function (res) {
            if (res.Result) {
                page.GetTRReturnUFBillData();
            }
            displayMessage(res.Message, res.Result);
        });

    },
    //删除未排除的故障
    DaleteTRReturnUFBillData: function () {
        var ids = [];
        var rows = $('#WPCTable').datagrid("getChecked");
        $.each(rows, function (index, item) {
            ids.push(item.ZSTRRETURNUFBILLID);
        });
        var param = {
            action: "DaleteTRReturnUFBillData",
            ids: ids.join(',')
        };
        if (ids.length == 0) { displayMessage("请勾选记录再进行操作", false); return; }
        page.post("1", param, function (res) {
            if (res.Result) {
                page.GetTRReturnUFBillData();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //查询重置
    searchReset: function () {
        $('#searchForm').form('clear');
        page.bind();
    },

    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },
    //post数据
    post: function (url, param, callback) {
        $.ajax({
            type: "Post",
            url: url == "" ? page.ajaxUrl : page.ajaxUrl2,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    }
}
$(function () {
    page.init();
});